﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using BabyFood.Data;
using BabyFood.Entities;

public partial class Admin_TinTuc_qlnhomtin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCatalog.BindNhomTin(cboNhomTin);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }

    protected void Bind_Data(int PageNo)
    {
        try
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            if (Common.StandardString(txtTieuDe.Text).Length > 0)
                WhereClause += " AND TieuDe LIKE N'" + Common.StandardString(txtTieuDe.Text) + "%'";
            if (cboNhomTin.SelectedValue != "0")
                WhereClause += string.Format(" AND IdLoaiTinTuc={0}", cboNhomTin.SelectedValue);
            DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("Custom_TinTuc_GetPaged",WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsTinTuc.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;

        }
        catch (Exception ex)
        {
            Common.WriteLog("Admin/TinTuc/QLTinTuc.aspx.cs", "Bind_Data", ex.ToString());
        }
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        Bind_Data(currnetPageIndex);
    }

    protected void cmdTimKiem_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Bind_Data(Pager1.CurrentIndex);
	    }
	    catch (Exception ex)
	    {
            Common.WriteLog("Admin/TinTuc/QLTinTuc.aspx.cs", "cmdTimKiem_Click", ex.ToString());
	    }
    }

    protected void cmdTimLai_Click(object sender, ImageClickEventArgs e)
    {
        cboNhomTin.SelectedValue = "0";
        txtTieuDe.Text = string.Empty;
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

                HiddenField H_IdCatalog = (HiddenField)e.Row.FindControl("H_IdCatalog");
                e.Row.Attributes.Add("ondblclick", "Javascript:DoubleClick_TinTuc('" + H_IdCatalog.Value + "');");
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("Admin/TinTuc/QLTinTuc.aspx.cs", "gridCatalog_RowDataBound", ex.ToString());
        }
    }

    protected void cmdHieuChinh_Click(object sender, ImageClickEventArgs e)
    {
        HiddenField H_IdCatalog = null;
        for (int i = 0; i < gridCatalog.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                break;
            }
        }
        if (H_IdCatalog != null)
        {
            Response.Redirect("~/Admin/TinTuc/CNTinTuc.aspx?id=" + H_IdCatalog.Value);
        }
    }

    protected void cmdXoa_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gridCatalog.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                HiddenField H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                HiddenField H_Image = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_Image");
                try
                {
                    DataRepository.TinTucProvider.Delete(Convert.ToInt32(H_IdCatalog.Value));
                    string imgNewsPath = Server.MapPath(Common.sPathUpTinTuc);
                    if (File.Exists(imgNewsPath + "\\" + H_Image.Value))
                    {
                        File.Delete(imgNewsPath + "\\" + H_Image.Value);
                    }
                }
                catch (Exception) { }
            }
        }
        Bind_Data(Pager1.CurrentIndex);
    }
}
